package com.lzw.service.impl;

import com.lzw.domain.dto.SQLFormat;
import com.lzw.domain.vo.ResponseVO;
import com.lzw.mapper.BoxmessageMapper;
import com.lzw.service.Line2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class Line2ServiceImpl implements Line2Service {

    @Autowired
    private BoxmessageMapper boxmessageMapper;

    //生产数量
    private Integer productionNum;

    //合格数量
    private Integer qualifiedNum;

    //不合格数量
    private Integer unqualifiedNum;

    //合格率
    private Double percentOfPass;

    @Override
    public List<ResponseVO> get15dayMsg(String procedureName) {
        //返回结果集
        List<ResponseVO> returnList = new ArrayList<>();
        //获取查询结果
        List<SQLFormat> listFromSQL = boxmessageMapper.get15dayMsg(procedureName);
        //遍历集合
        for (int i = 0; i < listFromSQL.size(); ) {
            ResponseVO responseVO = new ResponseVO();
            responseVO.setT(listFromSQL.get(i).getT());
            responseVO.setAlias(listFromSQL.get(i).getAlias());
            //因为查询结果为两条一组，偶数为生产数量，奇数为合格数量
            if (i % 2 == 0) { //生产数量
                if (listFromSQL.get(i).getValue() == 0 || listFromSQL.get(i).getValue() == null) {
                    //如果生产数量为0或空，则其余全为0
                    responseVO.setProductionNum(0);
                    responseVO.setQualifiedNum(0);
                    responseVO.setUnqualifiedNum(0);
                    responseVO.setPercentOfPass(0.0);
                    //添加到返回值中
                    returnList.add(responseVO);
                    i += 2;
                    continue;
                } else {
                    //生产数量不为0,则计算其他数值
                    if (listFromSQL.get(i + 1).getName() == "合格数量" || "合格数量".equals(listFromSQL.get(i + 1).getName())) {
                        productionNum = listFromSQL.get(i).getValue();
                        responseVO.setProductionNum(productionNum);

                        qualifiedNum = listFromSQL.get(i + 1).getValue();
                        responseVO.setQualifiedNum(qualifiedNum);

                        unqualifiedNum = productionNum - qualifiedNum;
                        responseVO.setUnqualifiedNum(unqualifiedNum);

                        //合格率=合格数量/生产数量，保留两位小数
                        BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
                        BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
                        BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
                        percentOfPass = divide.doubleValue();
                        responseVO.setPercentOfPass(percentOfPass);
                        returnList.add(responseVO);
                        i += 2;
                    } else {
                        //返回值只有生产数量和不合格数量
                        productionNum = listFromSQL.get(i).getValue();
                        responseVO.setProductionNum(productionNum);
                        unqualifiedNum = listFromSQL.get(i + 1).getValue();
                        responseVO.setUnqualifiedNum(unqualifiedNum);
                        qualifiedNum = productionNum - unqualifiedNum;
                        responseVO.setQualifiedNum(qualifiedNum);
                        //合格率=合格数量/生产数量，保留两位小数
                        BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
                        BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
                        BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
                        percentOfPass = divide.doubleValue();
                        responseVO.setPercentOfPass(percentOfPass);
                        returnList.add(responseVO);
                        i += 2;
                    }

                }
            }
        }

        return returnList;
    }

    @Override
    public ResponseVO getMsg(String procedureName) {
        //获取查询结果
        List<SQLFormat> lists = boxmessageMapper.getMsg(procedureName);
        //封装返回对象
        ResponseVO responseVO = new ResponseVO();
        //封装工序名称
        responseVO.setAlias(lists.get(0).getAlias());
        //封装日期
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        responseVO.setT(formatter.format(date));
        //如果生产数量为0，则其余数据全部为0，并返回
        if (lists.get(0).getValue() == 0) {
            responseVO.setProductionNum(0);
            responseVO.setQualifiedNum(0);
            responseVO.setUnqualifiedNum(0);
            responseVO.setPercentOfPass(0.0);
            return responseVO;
        }
        //封装 生产数量 和 不合格数量
        for (SQLFormat list : lists) {
            if (list.getName() == "生产数量" || "生产数量".equals(list.getName())) {
                responseVO.setProductionNum(list.getValue());
                productionNum = list.getValue();
            }
            if (list.getName() == "不合格数量" || "不合格数量".equals(list.getName())) {
                responseVO.setUnqualifiedNum(list.getValue());
                unqualifiedNum = list.getValue();

                //合格数量=生产数量-不合格数量
                qualifiedNum = productionNum - unqualifiedNum;
                responseVO.setQualifiedNum(qualifiedNum);
            } else if (list.getName() == "合格数量" || "合格数量".equals(list.getName())) {
                responseVO.setQualifiedNum(list.getValue());
                qualifiedNum = list.getValue();

                //不合格数量=生产数量-合格数量
                unqualifiedNum = productionNum - qualifiedNum;
                responseVO.setUnqualifiedNum(unqualifiedNum);
            }
        }

        //合格数量=生产数量-不合格数量
//        qualifiedNum = productionNum - unqualifiedNum;
//        responseVO.setQualifiedNum(qualifiedNum);
        //合格率=合格数量/生产数量，保留两位小数
        BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
        BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
        BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
        percentOfPass = divide.doubleValue();
        responseVO.setPercentOfPass(percentOfPass);
        return responseVO;
    }


}
